<template>
    <div>
        <p class="login">
            <el-tabs v-model="activeName" @tab-click="handleClick">
                <el-tab-pane label="登录" name="first">
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                        <el-form-item label="名称" prop="username"><el-input v-model="ruleForm.username"></el-input></el-form-item>

                        <el-form-item label="密码" prop="password"><el-input type="password" v-model="ruleForm.password" auto-complete="off"></el-input></el-form-item>

                        <el-form-item>
                            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>

                            <el-button @click="resetForm('ruleForm')">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-tab-pane>

                <el-tab-pane label="注册" name="second">
                    <register></register>
                </el-tab-pane>
            </el-tabs>
        </p>
    </div>

</template>

<script>
    import register from '../components/Register.vue';

    export default {
        name: 'Login',
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };

            return {
                activeName: 'first',
                ruleForm: {
                    username: '15729003571',
                    password: '123',
                    checkPass: ''
                },
                rules: {
                    username: [{ required: true, message: '请输入您的名称', trigger: 'blur' }, { min: 6, max: 11, message: '长度在 6 到 11 个字符', trigger: 'blur' }],
                    password: [{ required: true, validator: validatePass, trigger: 'blur' }]
                }
            };
        },

        methods: {
            //选项卡切换
            handleClick(tab, event) {},
            //重置表单
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            //提交表单
            submitForm(formName) {
                this.$refs[formName].validate(valid => {
                    if (valid) {
                        this.$http.post("oauth/oauth/token", {
                            grant_type: 'password',
                            username: this.ruleForm.username,
                            password: this.ruleForm.password,
                            client_id: 'manger',
                            client_secret: '123',
                        }).then(data => {
                            if (data) {
                                this.$message({
                                    type: 'success',
                                    message: '登录成功'
                                });
                                console.log(data)
                                var token = data["access_token"];
                                sessionStorage.setItem("token", token);
                                sessionStorage.setItem("username", this.ruleForm.username);
                                //sessionStorage.setItem("principal", JSON.stringify(data.principal));
                                this.$router.push("/product")
                            } else {
                                this.$message({
                                    type: 'info',
                                    message: '登录失败，用户名或密码错误'
                                });
                            }
                        })
                    }
                })
            }
        },
        components: {
            register
        }
    };
</script>

<style scoped="scoped">
    .login {
        width: 400px;
        margin: 0 auto;
    }

    .el-tabsitem {
        text-align: center;
        width: 60px;
    }
</style>
